package com.cskaoyan.javase.homework.senior._06reflect;

import java.lang.reflect.Constructor;
import java.util.Scanner;

/**
 * @description:
 * @author: 景天
 * @date: 2022/4/1 15:24
 **/

public class StudentTest {
    public static void main(String[] args) throws Exception {

        System.out.println("请输入学生信息(格式 名字:年龄:分数):");
        // 键盘录入信息
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();

        // 利用split分隔
        String[] split = s.split(":");
        String name = split[0];
        int age = Integer.parseInt(split[1]);
        int score = Integer.parseInt(split[2]);
        // 获取字节码文件对象
        Class<?> stuCls = Class.forName("homework._06reflect.Student");
        // 获取构造方法
        Constructor<?> declaredConstructor = stuCls.getDeclaredConstructor(String.class, int.class, int.class);
        // 实例化对象
        Student student = (Student) declaredConstructor.newInstance(name, age, score);
        // 打印
        System.out.println(student);
    }
}

class Student{
    String name;
    int age;
    int  score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public Student() {
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}